
/* -----------------
* brief 
* 1. this is code gen by tools
*/

package service

import (
	"errors"
	mchcorev2 "github.com/chanxuehong/wechat/mch/core"
	"github.com/chanxuehong/wechat/mchv3/marketing/busifavor"
	"github.com/mjiulee/lego"
	"saas_sys_base/common/url_const"
	. "saas_sys_base/modules/config/service"
	"sync"

	"saas_sys_base/common"
	. "saas_sys_base/modules/wechat/models"
)

type WxCouponService struct {
	// add your model here
	wechatSdkClient WechatSdkClient
	appConfigService AppConfigService
}

var once sync.Once

func init() {


}

func (t *WxCouponService) CreateCouponByWho(who *common.Who, couponPara *busifavor.MchCoupon) (*busifavor.CreateBusiFavorStockResp, error) {
	mchv3client, err := t.wechatSdkClient.GetWeappMchv3ClientByWho(t.appConfigService.ToUserMiniApp(who))
	if err != nil {
		return nil, err
	}

	appconfig, err := t.appConfigService.GetConfigByWho(*t.appConfigService.ToUserMiniApp(who))
	if err != nil {
		return nil, err
	}

	//once.Do(func() {
		_, err = t.SetCouponCallback(who, "")
		if err != nil {
			panic(err)
		}
	//})
	req := new(busifavor.CreateBusiFavorStockRequest)
	req.MchCoupon = *couponPara
	req.NotifyConfig = &busifavor.NotifyConfig{NotifyAppid: appconfig.App_Config_Data.Wechat.WX_APPID}

	resp, _,  err := busifavor.CreateBusiFavorStock(mchv3client, req)
	if err != nil {
		lego.LogError(err)
	}

	return resp, err
}

func (t *WxCouponService) GetCouponSign(who *common.Who, sendCouponParams []mchcorev2.SendCouponParams, sendCouponMerchant string) (string, error) {
	mchv2client, err := t.wechatSdkClient.GetWeappMchClientByWho(*who)
	if err != nil {
		return "", err
	}

	signStr := mchcorev2.CouponSignWrapper(mchv2client, sendCouponParams, sendCouponMerchant)
	if signStr == "" {
		return "", errors.New("GetCouponSign fail")
	}

	return signStr, err
}

func (t *WxCouponService) SetCouponCallback(who *common.Who, url string) (string, error) {
	var notifyUrl string
	mchv3client, err := t.wechatSdkClient.GetWeappMchv3ClientByWho(t.appConfigService.ToUserMiniApp(who))
	if err != nil {
		return "", err
	}

	appconfig, err := t.appConfigService.GetConfigByWho(*t.appConfigService.ToUserMiniApp(who))
	if err != nil {
		return "", err
	}

	req := busifavor.SetCallbackRequest{}
	mchid := appconfig.App_Config_Data.Wechat.WX_MERCHANT_ID
	appid := appconfig.App_Config_Data.Wechat.WX_APPID

	if url == "" {
		notifyUrl = appconfig.App_Config_Data.Wechat.WX_PAY_NOTIFY_URL + url_const.GenMchv3CallbackUrl(mchid, appid)
	} else {
		notifyUrl = url
	}

	req.Mchid = mchid
	req.NotifyUrl = notifyUrl
	_, _, err = busifavor.SetCallback(mchv3client, &req)
	if err != nil {
		return "", err
	}

	return notifyUrl, err
}

func (t *WxCouponService) GetCouponCallback(who *common.Who) (*busifavor.GetCallbackResp, error) {
	mchv3client, err := t.wechatSdkClient.GetWeappMchv3ClientByWho(t.appConfigService.ToUserMiniApp(who))
	if err != nil {
		return nil, err
	}

	appconfig, err := t.appConfigService.GetConfigByWho(*t.appConfigService.ToUserMiniApp(who))
	if err != nil {
		return nil, err
	}


	mchid := appconfig.App_Config_Data.Wechat.WX_MERCHANT_ID

	req := busifavor.GetCallbackUrlPara{}
	req.Mchid = mchid
	resp, _, err := busifavor.GetCallback(mchv3client, &req)
	if err != nil {
		return resp, err
	}

	return resp, err
}